<template>
    <rx-dialog @ok="handleCompleteCheck" @cancel="closeWindow" :okText="$ti18('rxForm.commitBtn','提交')"
               :btnAlign="'center'">
        <template #leftToolbar>
            <a-button @click="temporaryOpinion">{{ i18n('taskCheck.btnProvisional', '暂存意见') }}</a-button>
        </template>
        <rx-fit>
            <template #middle>
                <div style="height:100%;width:100%;padding:20px;">
                    <a-form class="rx-a-form-vertical" layout="vertical">
                        <a-form-item :label="i18n('taskTransferCheck.appAction','审批动作')">
                            <a-radio-group v-model:value="checkType" @change="checkTypeChange">
                                <a-radio value="AGREE">{{ i18n('taskTransferCheck.agree', '同意') }}</a-radio>
                                <a-radio value="REFUSE">{{ i18n('taskTransferCheck.disagree', '不同意') }}</a-radio>
                            </a-radio-group>
                        </a-form-item>
                        <a-form-item :label="i18n('taskTransferCheck.aboutToFlow','即将流向')">
                            <bpm-task-executors ref="taskExecutors" :nodeExecutors="nodeExecutors"
                                                :allow-select-executor="false"></bpm-task-executors>
                        </a-form-item>
                        <a-form-item :label="i18n('taskCheck.opinion','意见')">
                            <a-select
                                show-search
                                :value="value"
                                style="width: 200px"
                                :default-active-first-option="false"
                                :show-arrow="false"
                                :filter-option="false"
                                :not-found-content="null"
                                option-label-prop="label"
                                @search="handleSearch"
                                @change="handleChange"
                                @blur="handleBlur"
                                :placeholder="i18n('taskTransferCheck.opinionDesc','输入关键字搜索常用意见')"
                            >
                                <a-select-option v-for="d in data" :value="d.opText" :label="d.opText">
                                    <CloseOutlined @click.stop="closeOpinion(d)"/>
                                    {{ d.opText }}
                                </a-select-option>
                            </a-select>
                            <a-button style="margin-left: 6px;" @click="saveOpinion">
                                {{ $ti18('rxForm.saveBtn', '保存') }}
                            </a-button>
                            <a-textarea :placeholder="i18n('taskTransferCheck.opinionInputDesc','请填写意见，可保存为常用意见')"
                                        v-model:value="opinion" style="margin-top: 8px;"></a-textarea>
                        </a-form-item>

                        <a-form-item :label="i18n('taskCheck.attachment','附件')">
                            <rx-attach-component v-model="fileList" class="uploader-btn-ctl"></rx-attach-component>
                        </a-form-item>
                    </a-form>
                </div>
            </template>
        </rx-fit>
    </rx-dialog>
</template>
<script>
import {RxDialog, Util, RxFit} from '@lowcode/jpaas-base-lib'
import {RxUserComponent, RxAttachComponent} from '@lowcode/jpaas-share-component'
import BpmtaskApi from '@/common/api/bpm/bpmTask'
import BpmOpinionLibApi from "@/common/api/bpm/bpmOpinionLib";
import BpmTaskExecutors from "../../components/BpmTaskExecutors.vue";

export default {
    name: 'bpm-task-transfer-check',
    components: {
        RxUserComponent,
        BpmTaskExecutors,
        RxAttachComponent,
        RxFit,
        RxDialog
    },
    props: {
        layerid: String,
        destroy: Function,
        taskId: {
            type: String,
            required: true
        },
        formJson: String,
        systemHand: Boolean,
        nodeExecutors: Array,

        opinionObj: {
            type: Object,
            default: {
                name: "",
                value: ""
            }
        }
    },
    data() {
        return {
            labelCol: {span: 4},
            wrapperCol: {span: 8},
            checkType: 'AGREE',
            opinion: '',
            msgTypes: [],
            copyUsers: [],
            opinionName: "",
            value: undefined,
            data: [],
            fileList: [],
            temporaryOpinionObj: {}
        }
    },
    created() {
        this.opinion = this.opinionObj.value;
        this.opinionName = this.opinionObj.name;
        this.getTemporaryOpinion();
        this.fetch('', data => (this.data = data));
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "bpmTask", key);
        },
        checkTypeChange(e) {
            BpmtaskApi.getTaskFlowNodesExecutors(this.taskId, e.target.value, JSON.parse(this.formJson), this.systemHand).then(res => {
                this.nodeExecutors = res;
            })
        },
        handleCompleteCheck(e) {
            let self = this;
            var ctl = this.$refs.taskExecutors;
            //抄送用户
            let copyUserIds = this.getCopyUserId();
            let noticeTypes = this.msgTypes.join(",");
            let nodeUserMap = ctl.getNodeUserMap();
            let destNodeId = ctl.getDestNodeId();
            let cmd = {
                taskId: this.taskId,
                checkType: this.checkType,
                opinion: this.opinion,
                msgTypes: noticeTypes,
                copyUserAccounts: copyUserIds,
                nodeExecutors: nodeUserMap,
                formJson: self.formJson,
                systemHand: self.systemHand,
                opinionName: self.opinionName,
                opFiles: JSON.stringify(self.fileList)
            }
            //设置目标节点。
            if (destNodeId) {
                cmd.destNodeId = destNodeId;
            }
            BpmtaskApi.checkComplete(cmd).then(resp => {
                e.loading = false;
                if (resp.success) {
                    Util.closeWindow(self, 'ok');
                }
            })
        },


        /**
         * 获取抄送用户。
         * @returns {string}
         */
        getCopyUserId() {
            var aryUserId = [];
            if (this.copyUsers && this.copyUsers.length > 0) {
                this.copyUsers.forEach(item => {
                    aryUserId.push(item.account);
                })
            }
            return aryUserId.join(",");
        },
        closeWindow() {
            Util.closeWindow(this, 'cancel')
        },
        saveOpinion() {
            var self = this;
            BpmOpinionLibApi.save({opText: this.opinion}).then(res => {
                if (res.success) {
                    self.handleBlur();
                }
            });
        },
        closeOpinion(opinion) {
            let self_ = this;
            this.$confirm({
                title: this.$ti18('rxMsa.actionTips', '操作提示'),
                zIndex: 9999,
                content: self_.i18n('taskCheck.sureToClear', '确定要删除此条意见吗？'),
                okText: this.$ti18('rxForm.ackBtn', '确认'),
                cancelText: this.$ti18('rxForm.cancelBtn', '取消'),
                onOk() {
                    BpmOpinionLibApi.del({ids: opinion.opId}).then(res => {
                        self_.fetch('', data => (self_.data = data));
                    })
                },
                onCancel() {
                }
            })
        },
        fetch(value, callback) {
            BpmOpinionLibApi.getUserText(value).then(res => {
                callback(res);
            })
        },
        handleSearch(value) {
            this.value = value;
            this.fetch(value, data => (this.data = data));
        },
        handleChange(value) {
            this.value = value;
        },
        handleBlur() {
            if (this.value) {
                this.opinion = this.value;
                this.value = undefined;
            }
        },
        //暂存意见
        temporaryOpinion() {
            var id = this.temporaryOpinionObj.id ? this.temporaryOpinionObj.id : "";
            if (this.taskId) {
                if (this.opinion) {
                    BpmtaskApi.temporaryOpinion(this.taskId, this.opinion, id).then(res => {
                        Util.closeWindow(this, 'ok');
                    });
                } else {
                    this.$message.warning(this.i18n('taskCheck.noNullOpin', "意见不能为空"));
                    return
                }
            }
        },
        //获取暂存意见
        getTemporaryOpinion() {
            if (this.taskId) {
                BpmtaskApi.getTemporaryOpinion(this.taskId).then(res => {
                    if (res) {
                        this.opinion = res.opinion;
                        this.temporaryOpinionObj = res;
                    }
                });
            }
        }
    }
}
</script>
<style scoped>
.uploader-btn-ctl ::v-deep(.uploader-btn){
    padding: 5px 6px;
}
.rx-a-form-vertical ::v-deep(.upload-btn-box) .uploader-btn:last-child{
    margin-left: 0;
}
</style>
